<template>
  <div class="content" ref="content">
    <div class="table-default" v-if="tableData">
      <el-button type="primary" class="backBtn" @click="backHandler">返回</el-button>
      <el-button type="warning" class="backBtn" @click="approveHandler(route.query.id)" v-if="tableData.state == '未审核'" v-loading.fullscreen.lock="fullscreenLoading"
        ><Icon name="wrench-screwdriver" />审核</el-button
      >
      <!-- <div class="search-hidden" ref="searchbox"> -->
      <div id="base" ref="base">
        <el-descriptions :column="3" border title="基本信息">
          <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="采购退货单号">{{ tableData.returnnumber }}</el-descriptions-item>
          <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="销售出库单号">{{ tableData.fbillno }}</el-descriptions-item>
          <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="组织间关系">{{ tableData.incomingtype == 1 ? '组织间' : '组织外' }}</el-descriptions-item>
          <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="状态">{{ tableData.state }}</el-descriptions-item>
          <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="备注">{{ tableData.remark }}</el-descriptions-item>
          <el-descriptions-item label-class-name="labelClassNAme" class-name="contentClassName" label="回写状态">{{ tableData.writestate }}</el-descriptions-item>
        </el-descriptions>
      </div>
      <!-- </div> -->
      <div id="details" ref="details">
        <el-tabs v-model="activeName" class="demo-tabs" @tab-change="tabClick">
          <el-tab-pane label="采购退货-明细" name="details">
            <detailTableVue :tableData="tableData.details" :state="tableData.state" :tabName="'details'"></detailTableVue>
            <Paginate />
          </el-tab-pane>

          <el-tab-pane label="采购退货-汇总" name="summary">
            <detailTableVue :tableData="detailData.details" :tabName="'summary'"></detailTableVue>
            <Paginate />
          </el-tab-pane>
        </el-tabs>
      </div>
      <!-- </el-scrollbar> -->
    </div>
  </div>
</template>
<script lang="ts" setup>
import { computed, onMounted, ref } from 'vue'
import { useGetList } from '/admin/composables/curd/useGetList'
import { useDestroy } from '/admin/composables/curd/useDestroy'
import { useOpen } from '/admin/composables/curd/useOpen'
import { useRouter, useRoute } from 'vue-router'
import detailTableVue from './detaiTable.vue'
import http from '/admin/support/http'
import { ElMessage } from 'element-plus'
import emitter from '/admin/utils/emitter'

const router = useRouter()
const route = useRoute()

const api = `purchasing/purchasereturn/${route.query.id}`
const { data, query, search, reset, loading } = useGetList(api)
const { destroy, deleted } = useDestroy()
const { open, close, title, visible, id } = useOpen()

const tableData = computed(() => data.value?.data.data)

const tabName = ref('')

console.log(tableData)

const detailData = ref([])

// const state = ref(route.query.state)

const getDetail = () => {
  const api2 = `purchasing/purchasereturn/${route.query.id}?detailsAll=true`
  http.get(api2).then(r => {
    const res = r.data.data
    console.log('汇总数量', res)
    detailData.value = res.data
  })
}

getDetail()

const tabClick = tab => {
  console.log(tab)
  tableData.value = detailData.value
}
const fullscreenLoading = ref(false)

const approveHandler = id => {
  fullscreenLoading.value = true

  http
    .put(`/purchasing/purchasereturnexamine/${id}`)
    .then(r => {
      fullscreenLoading.value = false
      ElMessage({
        message: r.data.message,
        type: 'success',
      })
      emitter.emit('changepurchasereturnStatus', { status: true })
      search()
    })
    .catch(error => {
      fullscreenLoading.value = false
    })
}

const stateFormatter = ref({
  0: '未审核',
  1: '已审核',
})

const searchbox = ref(null)
const showRef = ref(null)
const flag = ref(false)

console.log('路由参数接收', route.query.id)

const backHandler = () => {
  router.push('/purchasing/purchasereturn')
}
const isMore = ref<boolean>(false)

const activeName = ref('details')
const base = ref(null)
const details = ref(null)
const content = ref()

onMounted(() => {
  search()
  deleted(reset)
})
</script>

<style lang="css">
/* .content{
  border: 2px solid red;
 overflow:hidden;
} */
.backBtn {
  margin: 20px 10px;
}

h2 {
  font-weight: bold;
}
#base {
  margin-bottom: 30px;
  /* border: 2px solid green; */
}
#details {
  /* border: 2px solid pink; */
}

.tabs {
  padding: 10px 0 0;
  /* border: 2px solid #000; */
  z-index: 999;
  background-color: #fff;
  position: sticky;
  top: -15px;
}

.tagRow {
  display: inline-block;
  /* border: 1px solid #000; */
  margin: 20px 10px;
}

.tagRow ul {
  display: flex;
  border: 1px solid #eee;
}
.tagRow ul li {
  margin: 0 5px;
  /* border: 1px solid #eee; */
  padding: 5px 10px;
  /* border-radius: 5px; */
}
.active {
  background-color: #409eff;
  color: #fff;
}
.labelClassNAme {
  width: 130px;
}
.contentClassName {
  width: 170px;
}
</style>
